package com.shujia.sink

import java.time.Duration

import org.apache.flink.api.common.serialization.SimpleStringEncoder
import org.apache.flink.configuration.MemorySize
import org.apache.flink.streaming.api.scala.{DataStream, StreamExecutionEnvironment}
import org.apache.flink.core.fs.Path
import org.apache.flink.streaming.api.functions.sink.filesystem.StreamingFileSink
import org.apache.flink.streaming.api.functions.sink.filesystem.rollingpolicies.DefaultRollingPolicy

object Demo1FileSInk {
  def main(args: Array[String]): Unit = {

    val env: StreamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment

    val socketDS: DataStream[String] = env.socketTextStream("master", 8888)

    /**
      * 1、将数据保存到文件中
      *
      */
    //socketDS.writeAsText("data/flink_text")


    val sink: StreamingFileSink[String] = StreamingFileSink
      .forRowFormat(new Path("data/flink_text"), new SimpleStringEncoder[String]("UTF-8"))
      .withRollingPolicy(
        DefaultRollingPolicy.builder()
          .withRolloverInterval(Duration.ofSeconds(10))
          .withInactivityInterval(Duration.ofSeconds(10))
          .withMaxPartSize(MemorySize.ofMebiBytes(1))
          .build())
      .build()


    socketDS.addSink(sink)


    env.execute()
  }

}
